StringBuilder
StringBuilder used to create and manipulate dynamic string information---i.e., mutable strings. Every StringBuilder can store a certain number of characters that is specified by its capacity. Exceeding the capacity of a StringBuilder causes the capcacity to expand to accommodate the additional characters.
Constructors:
StringBuilder()
StringBuilder(size)
StringBuilder(object)
StringBuilder(object,start,numchars,maximum)
Methods:
StringBuilder Append(“object”)
StringBuilder AppendFormat(“format”,value)
StringBuilder AppendLine(“String”)
int Capacity()
int MaxCapacity()
void EnsureCapacity(size)
StringBuilder Remove(char ,numof chars)
StringBuilder Replace(string1,string2)
Program on StringBuilder Constructors
using System;
using System.Text;
class sample
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder(20);// 20 is the capacity of string
StringBuilder sb2 = new StringBuilder("Prasad");
StringBuilder sb3 = new StringBuilder("prasAD", 2, 3,10);
Console.WriteLine(sb);
Console.WriteLine(sb1);
Console.WriteLine(sb2);
Console.WriteLine(sb3);
}
}
Program on StringBuilder Class Methods
using System;
using System.Text;
class sample
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("Prasad");
sb.Append(20);
Console.WriteLine(sb);
sb.AppendFormat("{0:e}", 3456);
Console.WriteLine(sb);
StringBuilder sb1 = sb.AppendLine("Welcome to vision");
Console.WriteLine(sb1);
Console.WriteLine(sb.Capacity);
Console.WriteLine(sb.MaxCapacity);
sb.EnsureCapacity(20);
Console.WriteLine(sb.Capacity);
sb.Length = 5;
Console.WriteLine(sb);
}
}